//
//  BCOrderFirstTableViewCell.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/7/19.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCOrderFirstTableViewCell.h"

@interface BCOrderFirstTableViewCell () <UITextFieldDelegate,MMNumberKeyboardDelegate>

@end

@implementation BCOrderFirstTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    _countTextFeild.delegate = self;
    
    _typeButton.layer.cornerRadius = 3.f;
    
    // Create and configure the keyboard.
    MMNumberKeyboard *keyboard = [[MMNumberKeyboard alloc] initWithFrame:CGRectZero];
    keyboard.returnKeyTitle = @"确认";
    NSDictionary *dic = [keyboard valueForKey:@"buttonDictionary"];
    [dic[@(12)] setValue:BangcaiGreen forKey:@"fillColor"];
    keyboard.allowsDecimalPoint = NO;
    keyboard.delegate = self;
    _countTextFeild.inputView = keyboard;
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40.f)];
    accessoryView.backgroundColor = [UIColor whiteColor];
    UILabel *keyboardTipLable = [[UILabel alloc] initWithFrame:accessoryView.bounds];
    keyboardTipLable.font = [UIFont systemFontOfSize:13];
    keyboardTipLable.textColor = BangcaiGreen;
    keyboardTipLable.text = @"请输入欲购买的数量";
    keyboardTipLable.textAlignment = NSTextAlignmentCenter;
    [accessoryView addSubview:keyboardTipLable];
    _countTextFeild.inputAccessoryView = accessoryView;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"OrderFirstCellIdentifer";
    UINib *nib = [UINib nibWithNibName:@"BCOrderFirstTableViewCell" bundle: [NSBundle mainBundle]];
    [tableView registerNib:nib forCellReuseIdentifier:ID];
    BCOrderFirstTableViewCell *cell = [[BCOrderFirstTableViewCell alloc] init];
    return cell;
}

- (void)makeAttributedString {
    
    NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
    STUFF *stuff_model = stuff_array.lastObject;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.priceLable.text];
    UIColor *textColor = [UIColor colorWithRed:0.937 green:0.388 blue:0.310 alpha:1.000];
    [attributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                             value:textColor
                             range:NSMakeRange(0, @"￥".length+[stuff_model.price rounding:2].length)];
    [attributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                             value:[UIColor colorWithRed:0.702 green:0.702 blue:0.702 alpha:1.000]
                             range:NSMakeRange(@"￥".length+[stuff_model.price rounding:2].length, @"/".length+stuff_model.unit.length)];
    UIFont *priceFont = [UIFont boldSystemFontOfSize:15.f];
    UIFont *weightFont = [UIFont systemFontOfSize:13.f];
    [attributedString addAttribute:NSFontAttributeName             //文字字体
                             value:priceFont
                             range:NSMakeRange(0, @"￥".length+[stuff_model.price rounding:2].length)];
    [attributedString addAttribute:NSFontAttributeName             //文字字体
                             value:weightFont
                             range:NSMakeRange(@"￥".length+[stuff_model.price rounding:2].length, @"/".length+stuff_model.unit.length)];
    self.priceLable.attributedText = attributedString;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)addAction:(UIButton *)sender {
    
    NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
    STUFF *stuff_model = stuff_array.lastObject;
    
    [[BCShopCarManager sharedInstance] addStuffName:self.stuffNameLable.text amount:1 price:stuff_model.price.doubleValue type:stuff_model.category.integerValue];
    [BCOrderViewManager updateAddStuffToOrder:stuff_model];
    
    if (self.countTextFeild.text.integerValue < 100) {
        _decButton.hidden = NO;
        _countTextFeild.hidden = NO;
        NSInteger count = self.countTextFeild.text.integerValue + 1;
        self.countTextFeild.text = [NSString stringWithFormat:@"%@",@(count)];
    }
    _reddot();
}

- (IBAction)decAction:(UIButton *)sender {
    if (self.countTextFeild.text.integerValue > 0) {
        NSInteger count = self.countTextFeild.text.integerValue - 1;
        
        NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
        STUFF *stuff_model = stuff_array.lastObject;
        
        [[BCShopCarManager sharedInstance] removeStuffName:self.stuffNameLable.text amount:1 price:stuff_model.price.doubleValue type:stuff_model.category.integerValue];
        [BCOrderViewManager updateDecStuffToOrder:stuff_model];
        
        if (count==0) {
            _decButton.hidden = YES;
            _countTextFeild.hidden = YES;
            return;
        }
        
        self.countTextFeild.text = [NSString stringWithFormat:@"%@",@(count)];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    _addButton.userInteractionEnabled = NO;
    _decButton.userInteractionEnabled = NO;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    _addButton.userInteractionEnabled = YES;
    _decButton.userInteractionEnabled = YES;
}

-(BOOL)numberKeyboardShouldReturn:(MMNumberKeyboard *)numberKeyboard {
    NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
    STUFF *stuff_model = stuff_array.lastObject;
    if (0 == _countTextFeild.text.integerValue) {
        _decButton.hidden = YES;
        _countTextFeild.hidden = YES;
        [BCOrderViewManager removeOrder:_uuid];
    }
    else {
        [BCOrderViewManager writeStuffUseCountToOrder:_uuid Count:_countTextFeild.text.integerValue];
    }
    
    [[BCShopCarManager sharedInstance] setStuffName:self.stuffNameLable.text amount:_countTextFeild.text.integerValue price:stuff_model.price.doubleValue type:stuff_model.category.integerValue];
    return YES;
}

@end
